package com.zb.springcloud.service.impl;

import com.zb.springcloud.dao.OrderDao;
import com.zb.springcloud.domain.Order;
import com.zb.springcloud.service.AccountService;
import com.zb.springcloud.service.OrderService;
import com.zb.springcloud.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Resource
    OrderDao orderDao;
    @Resource
    StorageService storageService;
    @Resource
    AccountService accountService;

    @Override
    @GlobalTransactional(name = "order", rollbackFor = Exception.class)
    public void create(Order order) {
        log.info("----------->开始新建订单");
        orderDao.create(order);

        log.info("----------->订单微服务开始调用库存，做扣减库存");
        storageService.decrease(order.getProductId(), order.getCount());
        log.info("----------->扣减成功");

        log.info("----------->订单微服务开始调用账号，做扣减金额");
        accountService.decrease(order.getUserId(), order.getMoney());
        log.info("----------->扣减成功");

//        修改订单的状态
        log.info("----------->修改订单状态开始");
        orderDao.update(order.getUserId(), 0);
        log.info("----------->修改订单状态结束");

        log.info("----------->下订单结束");
    }
}
